﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Data
{
    public class XMLParser
    {
        public void write(List<Opskrift> opskrifter)
        {
            XmlTextWriter xwrite = new XmlTextWriter("data.xml",null);
            xwrite.Formatting = Formatting.Indented;
            xwrite.WriteStartDocument();
            xwrite.WriteStartElement("OPSKRIFTER");
            foreach (Opskrift opsk in opskrifter)
            {
                xwrite.WriteStartElement("OPSKRIFT");
                xwrite.WriteAttributeString("titel", opsk.Titel);
                xwrite.WriteAttributeString("maengde", opsk.Maengde.ToString());
                xwrite.WriteAttributeString("enhed", opsk.Enhed);
                xwrite.WriteAttributeString("type", opsk.Type);
                foreach (Ingrediens ing in opsk.Ingredienser)
                {
                    xwrite.WriteStartElement("INGREDIENS");
                    xwrite.WriteAttributeString("inTitel", ing.Titel);
                    xwrite.WriteAttributeString("inMaengde", ing.Maengde.ToString());
                    xwrite.WriteAttributeString("inEnhed", ing.Enhed);
                    xwrite.WriteEndElement();
                }
                xwrite.WriteStartElement("FREMGANGSMAADE");
                xwrite.WriteString(opsk.Fremgangsmaade);
                xwrite.WriteEndElement();
                xwrite.WriteStartElement("NOTER");
                xwrite.WriteString(opsk.Noter);
                xwrite.WriteEndElement();
                foreach (Opskrift relOpsk in opsk.Relaterede.Values)
                {
                    xwrite.WriteStartElement("RELOPSKRIFT");
                    xwrite.WriteAttributeString("relTitel", relOpsk.Titel);
                    xwrite.WriteEndElement();
                }
                xwrite.WriteEndElement();
            }
            xwrite.WriteEndElement();
            xwrite.WriteEndDocument();
            xwrite.Flush();
            xwrite.Close();
        }

        public Dictionary<string,Opskrift> read()
        {
            Dictionary<string, Opskrift> list = new Dictionary<string, Opskrift>();
            XmlTextReader xmlr = new XmlTextReader("data.xml");
            Opskrift op = null;
            while (xmlr.Read())
            {
                if (xmlr.NodeType == XmlNodeType.Element && xmlr.Name.Equals("OPSKRIFT"))
                {
                    op = new Opskrift();
                    op.Titel = xmlr.GetAttribute("titel");
                    op.Maengde = Double.Parse(xmlr.GetAttribute("maengde"));
                    op.Enhed = xmlr.GetAttribute("enhed");
                    op.Type = xmlr.GetAttribute("type");
                }
                if (xmlr.NodeType == XmlNodeType.Element && xmlr.Name.Equals("INGREDIENS"))
                {
                    Ingrediens ing = new Ingrediens();
                    ing.Titel = xmlr.GetAttribute("inTitel");
                    ing.Maengde = Double.Parse(xmlr.GetAttribute("inMaengde"));
                    ing.Enhed = xmlr.GetAttribute("inEnhed");
                    op.Ingredienser.Add(ing);
                }
                if (xmlr.NodeType == XmlNodeType.Element && xmlr.Name.Equals("FREMGANGSMAADE"))
                {
                    op.Fremgangsmaade = xmlr.ReadString();
                }
                if (xmlr.NodeType == XmlNodeType.Element && xmlr.Name.Equals("NOTER"))
                {
                    op.Noter = xmlr.ReadString();
                }
                if (xmlr.NodeType == XmlNodeType.Element && xmlr.Name.Equals("RELOPSKRIFT"))
                {
                    Opskrift rel = null;
                    if (list.TryGetValue(xmlr.GetAttribute("relTitel"), out rel))
                    {
                        op.Relaterede.Add(rel.Titel, rel);
                        rel.Relaterede.Add(op.Titel, op);
                    }
                }
                if (xmlr.NodeType == XmlNodeType.EndElement && xmlr.Name.Equals("OPSKRIFT"))
                {
                    list.Add(op.Titel, op);
                }
                
            }//EndWhile

            xmlr.Close();

            return list;
        }
    }
}
